import { UI_Layer } from '../../config/ui_def'
import { UI_Listen, RedDot_Key, Control_Listen } from '../../config/ui_def'
import { WX_AD_CONFIG } from '../../frame/constants'
import { getLongScreen } from '../tool/util'
cc.Class({
    extends: cc.Component,

    properties: {
        content_width: cc.Widget,

        bottom_node: cc.Node,
        mission: cc.Node,
        team: cc.Node,
        fight: cc.Node,
        card: cc.Node,
        bag: cc.Node,
        adNode: cc.Node,
    },
    initPage() {
        this.content_width.updateAlignment()
    },
    updatePage() {
        this.aniGrp = []
        this.bottomButtonInit()
        this.widgt = this.node.getComponent(cc.Widget)
        if (getLongScreen()) {
            this.widgt.bottom = 50
        } else {
            this.widgt.bottom = 0
        }
        gg.eventManager.on('bottomBar', this.listenClient, this)
    },

    closeUI() {
        this.clearAni()
        this.node.targetOff(this)
    },
    listenClient(code, param, arg) {
        switch (parseInt(code)) {
            case UI_Listen.TASK_TIAOZHUAN:
                this.bottomButtonServer(null, param)
                break
            case Control_Listen.UPDATE_RED_DOT:
                this.updateRed(param, arg)
                break
        }
    },

    updateRed(key, status) {
        let red_node
        switch (key) {
            case RedDot_Key.TASK:
                red_node = this.bottom_node.getChildByName('Mission').getChildByName('Background').getChildByName('red_dot')
                break;
            case RedDot_Key.ROLE_INFO:
                red_node = this.bottom_node.getChildByName('Team').getChildByName('Background').getChildByName('red_dot')
                break
        }
        red_node.active = status
    },



    //底5个按钮的初始状态
    bottomButtonInit() {
        this.wid1 = (this.content_width.node.width - 10 * 4) / (4 + 1.5)
        this.wid2 = this.wid1 * 1.5
        this.hei1 = this.wid1 * 1.1
        this.hei2 = this.wid2 * 1.1

        this.bottomButtonServer(null, 2, true)
    },
    //底下5个按钮的方法
    bottomButtonServer(event, prarm, nofunc) {
        // this.buttonIndex = prarm
        // this.showPage()
        // if (prarm == 3) {
        //     gg.uiManager.addTip('当前进度未解锁')
        //     return
        // }

        if (prarm == this.buttonIndex) return
        else {
            he.voice.playEffect("page", "voice")
            for (let i = 0; i < this.bottom_node.children.length; i++) {
                this.bottom_node.children[i].getComponent(cc.Button).interactable = false
            }
            for (let j = 0; j < this.bottom_node.children.length; j++) {
                if (j != this.buttonIndex && j != prarm) {
                    this.aniGrp[j] = cc.tween(this.bottom_node.children[j])
                        .to(0.2, { width: this.wid1, height: this.hei1 })
                        .start()
                }
            }
            if (this.buttonIndex) {
                this.bottom_node.children[this.buttonIndex].getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_zhuye_weixuanzhong')
                this.bottom_node.children[this.buttonIndex].getChildByName('Background').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(this.changeBtn(this.buttonIndex))
                // this.bottom_node.children[this.buttonIndex].getChildByName('Background').y -= 50
                // this.bottom_node.children[this.buttonIndex].getChildByName('Background').getChildByName('Label').color = cc.Color.BLACK
                this.bottom_node.children[this.buttonIndex].getChildByName('lab').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.labChangeName(this.buttonIndex)}1`)
                this.ani1 = cc.tween(this.bottom_node.children[this.buttonIndex])
                    .to(0.2, { width: this.wid1, height: this.hei1 })
                    // .call(() => {
                    // })
                    .start()

                this.ani3 = cc.tween(this.bottom_node.children[this.buttonIndex].getChildByName('Background'))
                    .by(0.2, { y: -40 })
                    .start()

            }

            //有个bug- 0-- - -- - - -
            this.ani2 = cc.tween(this.bottom_node.children[prarm])
                .call(() => {
                    this.bottom_node.children[prarm].getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_zhuye_xuanzhong')
                    let name = `${this.changeBtn(prarm)}2`
                    this.bottom_node.children[prarm].getChildByName('Background').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(name)
                    // this.bottom_node.children[prarm].getChildByName('Background').y += 50
                    let labName = `${this.labChangeName(prarm)}2`
                    this.bottom_node.children[prarm].getChildByName('lab').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(labName)
                    // this.bottom_node.children[prarm].getChildByName('Background').getChildByName('Label').color = cc.Color.YELLOW
                    this.ani4 = cc.tween(this.bottom_node.children[prarm].getChildByName('Background'))
                        .by(0.2, { y: +40 })
                        .start()
                })
                .to(0.2, { width: this.wid2, height: this.hei2 })
                .call(() => {
                    if (!this.buttonIndex) {
                        this.bottom_node.children[prarm].getChildByName('Background').y += 50
                        this.bottom_node.children[prarm].getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_zhuye_xuanzhong')
                        let name = `${this.changeBtn(prarm)}2`
                        // this.bottom_node.children[prarm].getChildByName('Background').y += 50
                        this.bottom_node.children[prarm].getChildByName('Background').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(name)
                        // this.bottom_node.children[prarm].getChildByName('Background').width = 101
                        // this.bottom_node.children[prarm].getChildByName('Background').height = 97
                        let labName = `${this.labChangeName(prarm)}2`
                        this.bottom_node.children[prarm].getChildByName('lab').getComponent(cc.Sprite).spriteFrame = he.load.get('mainNew', cc.SpriteAtlas, 'plist').getSpriteFrame(labName)
                        // this.bottom_node.children[prarm].getChildByName('Background').getChildByName('Label').color = cc.Color.YELLOW
                    }
                    this.buttonIndex = prarm
                    if (nofunc) {

                    } else {
                        this.showPage()
                    }
                    for (let j = 0; j < this.bottom_node.children.length; j++) {
                        this.bottom_node.children[j].getComponent(cc.Button).interactable = true
                    }
                    let layout = this.bottom_node.getComponent(cc.Layout)
                    // layout.updateLayout()
                })
                .start()
        }
    },
    //展示相应的页面
    showPage() {
        switch (parseInt(this.buttonIndex) + 3001) {
            case UI_Listen.BOTTOM_MISSION:
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_CLOSEPAGE, pageName: 'missionPage' })
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: 'missionPage',
                })
                he.local.click('mission')
                // gg.uiManager.addTip('开发中')
                break
            case UI_Listen.BOTTOM_TEAM:
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_CLOSEPAGE, pageName: 'equipmentPage' })
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: 'equipmentPage',
                    // name: 'equipmentPage',
                })
                break
            case UI_Listen.BOTTOM_FIGHT:
                he.local.click('fight')
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_TOMAIN })
                break
            case UI_Listen.BOTTOM_CARD:
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_CLOSEPAGE, pageName: 'skillPage' })
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: 'skillPage',
                })
                he.local.click('card')
                // gg.uiManager.addTip('开发中')
                break
            case UI_Listen.BOTTOM_BAG:
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_CLOSEPAGE, pageName: 'bagPage' })
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: 'bagPage',
                })
                he.local.click('bag')
                break
        }
    },

    changeBtn(index) {
        let picName = null
        switch (parseInt(index)) {
            case 0:
                picName = `icon_renwu`
                break
            case 1:
                picName = `icon_duiwu`
                break
            case 2:
                picName = `icon_zhandou`
                break
            case 3:
                picName = `icon_kapian`
                break
            case 4:
                picName = `icon_beibao`
                break
        }
        return picName
    },

    labChangeName(index) {
        let labName = null
        switch (parseInt(index)) {
            case 0:
                labName = `text_renwu`
                break
            case 1:
                labName = `text_duiwu`
                break
            case 2:
                labName = `text_zhandou`
                break
            case 3:
                labName = `text_kapian`
                break
            case 4:
                labName = `text_beibao`
                break
        }
        return labName
    },

    clearAni() {
        if (this.ani1) {
            this.ani1.stop()
            this.ani1 = null
        }
        if (this.ani2) {
            this.ani2.stop()
            this.ani2 = null
        }
        if (this.ani3) {
            this.ani3.stop()
            this.ani3 = null
        }
        if (this.ani4) {
            this.ani4.stop()
            this.ani4 = null
        }
        for (let i = 0; i < this.aniGrp.length; i++) {
            if (this.aniGrp[i]) {
                this.aniGrp[i].stop()
                this.aniGrp[i] = null
            }
        }
    },


    gachaServer() {
        gg.uiManager.addPage(UI_Layer.Cover, {
            name: 'gachaPage',
            close: true
        })
    },
});
